package com.getsendsocial.GetSendSocial.Util;

import java.util.ArrayList;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.os.Bundle;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockFragment;
import com.getsendsocial.GetSendSocial.R;
import com.getsendsocial.GetSendSocial.Activity.ABSTabsViewPagerActivity;
import com.getsendsocial.GetSendSocial.Database.PostString;
import com.getsendsocial.GetSendSocial.Model.FraseToPostModel;

public class AddTab extends SherlockFragment {
	public static String response;
	private EditText et;
	public static ProgressDialog pdialog;
	private LayoutInflater inf;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		inf = inflater;
		final View view = inflater.inflate(R.layout.add, null);	   	   
		et = (EditText)view.findViewById(R.id.addSentence);

		final Spinner spinner = (Spinner) view.findViewById(R.id.spinnerAdd);
		ArrayList<String> list = new ArrayList<String>();
		list.add("Frasi Simpatiche");
		list.add("Frasi Augurali");


		spinner.setAdapter(new MyAdapterSpinner(ABSTabsViewPagerActivity.c,android.R.layout.simple_spinner_item,list,inf));
		Button buttonAdd = (Button)view.findViewById(R.id.addButton);

		buttonAdd.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

				if(!checkInternetConnection()){
					if (et.getText().length()==0){
						Toast.makeText(ABSTabsViewPagerActivity.c, "Controlla di aver inserito la frase!", Toast.LENGTH_SHORT).show();
					}else{
						
						FraseToPostModel fraseToPost = new FraseToPostModel();
						fraseToPost.setText(et.getText().toString());
						if (String.valueOf(spinner.getSelectedItem()).equals("Frasi Simpatiche")){
							fraseToPost.setType(1);
						}else{
							fraseToPost.setType(0);
						}
						pdialog = new ProgressDialog(ABSTabsViewPagerActivity.c);
						pdialog.setMessage("Salvo la frase sul server..");
						pdialog.show();
						new PostString(ABSTabsViewPagerActivity.c).execute(fraseToPost);
						et.setText("");
					}

				}else {
					Toast.makeText(ABSTabsViewPagerActivity.c, "Devi avere una connessione dati disponibile per aggiornare il tuo database!", Toast.LENGTH_SHORT).show();
					startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
			}
			}

			private boolean checkInternetConnection() {
				ConnectivityManager cm = (ConnectivityManager) ABSTabsViewPagerActivity.c.getSystemService(Context.CONNECTIVITY_SERVICE);
				State mobile = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
				State wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
				if (mobile == NetworkInfo.State.DISCONNECTED && wifi == NetworkInfo.State.DISCONNECTED){
					return false;
				}
				return false;
			}
		});
		return view;
	}

}
